/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package job.manager.client.mb;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ValueChangeEvent;
import javax.faces.event.ValueChangeListener;
import javax.faces.model.SelectItem;
import job.manager.db.ec.JobEB;
import job.manager.db.ec.TimeScheduleEB;
import job.manager.db.ec.UsersEB;
import job.manager.db.sb.JobRemote;

/**
 *
 * @author Administrator
 */
public class CalendarMB implements ValueChangeListener {

    @EJB
    private JobRemote jobBean;
    private String highLights;
    private Date caldate;
    private Date jobStart;
    private Date jobEnd;
    private UsersEB user;
    private String test;
    private ArrayList<JobEB> jobs;
    private String jobName;
    private ArrayList<SelectItem> itemList;
    private String comment;
    private int noOfPersons;
    private Calendar helpCal;

    public CalendarMB() {

        onInit();

    }

    @PostConstruct
    private void onInit() {
        caldate = new Date();


    }

    public Date getJobEnd() {
        return jobEnd;
    }

    public void setJobEnd(Date jobEnd) {
        this.jobEnd = jobEnd;
    }

    public String getHighLights() {
        return highLights;
    }

    public void setHighLights(String highLights) {
        this.highLights = highLights;
    }

    public Date getJobStart() {
        return jobStart;
    }

    public void setJobStart(Date jobStart) {
        this.jobStart = jobStart;
    }

    public int getNoOfPersons() {
        return noOfPersons;
    }

    public void setNoOfPersons(int noOfPersons) {
        this.noOfPersons = noOfPersons;
    }

    public void setUser(UsersEB user) {
        this.user = user;
        if (user != null) {
            jobs = new ArrayList<JobEB>(jobBean.getJobsByProfile(user.getProfileId()));
            itemList = new ArrayList<SelectItem>();
            helpCal = new GregorianCalendar();
            for (JobEB j : jobs) {
                SelectItem a = new SelectItem();
                a.setLabel(j.getName());
                a.setValue(j.getId());
                itemList.add(a);
            }
            updateJobInfo(jobs.get(0).getId());
        }
        
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public Date getCalDate() {
        return caldate;
    }

    public void setCalDate(Date date) {
        this.caldate = date;
    }

    public ArrayList<SelectItem> getJobList() {

        return itemList;
    }

    public void setTest(String s) {
        this.test = s;
    }

    public String getTest() {
        return test;
    }

    public void processValueChange(ValueChangeEvent event) throws AbortProcessingException {
        if (user != null) {
            Integer i = Integer.parseInt(((String) event.getNewValue()));
            updateJobInfo(i);
        }

    }

    private void updateJobInfo(int jobid) {
        JobEB currentJob = jobBean.getJob(jobid);
        TimeScheduleEB currentSchedule = currentJob.getTimeScheduleId();
        setComment(currentJob.getComment());
        setNoOfPersons(currentJob.getNumberOfPeople());
        setJobStart(currentSchedule.getStartid().getDateTime());
        setJobEnd(currentSchedule.getEndId().getDateTime());
        setHighLights(createHighlight(currentSchedule.getStartid().getDateTime(), currentSchedule.getEndId().getDateTime()));
    }

    private String createHighlight(Date start, Date end) {
        String highString;
        helpCal.setTime(start);
        int startDate = helpCal.get(Calendar.DAY_OF_YEAR);
        helpCal.setTime(end);
        int endDate = helpCal.get(Calendar.DAY_OF_YEAR);
        highString = "" + startDate;
        for (int i = (startDate + 1); i <= endDate; i++) {
            highString += "," + i;
        }

        return highString;
    }
}
